﻿using System;
using System.Collections.Generic;
using System.Data;
using DM4;
using DevExpress.XtraGrid.Views.Base;

namespace DM4Client
{
    public partial class FrmMain
    {
        private void DgModulesFocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            RefreshOneModule();
        }
        
        private void RefreshModulesList(int groupId)
        {
            _fGroups.gvModules.FocusedRowChanged -= DgModulesFocusedRowChanged;
            try
            {
                try
                {
                    _fGroups.dgModules.DataSource = null;

                    XMLInfo xi = Dm4Srv.GetModulesList(groupId);
                    DtModules = XMLInfo.FillTable(xi, DtModules);

                    _fGroups.dgModules.DataMember = DtModules.TableName;
                    _fGroups.dgModules.DataSource = DtModules;

                    if (_fGroups.gvModules.RowCount > 0)
                        _fGroups.gvModules.MoveFirst();

                    RefreshOneModule();

                    UpdateMainControlsState();
                }
                catch (Exception ex)
                {
                    //SERVER IS DOWN!! NEED RECONNECT                    
                    WriteError(ex.Message);
                    LoginToService();
                }
            }
            finally
            {
                _fGroups.gvModules.FocusedRowChanged += DgModulesFocusedRowChanged;
            }
        }

        private void RefreshOneModule()
        {
            //get datarow           
            int rowHandle = _fGroups.gvModules.FocusedRowHandle;
            DataRow drs = _fGroups.gvModules.GetDataRow(rowHandle);

            //fill module info table 
            ShowModuleInfo(drs);

            UpdateMainControlsState();
        }

        private void ShowModuleInfo(DataRow drs)
        {
            //fill module info table 
            dsInfo.Tables["dtModuleInfo"].Rows.Clear();
            dsInfo.Tables["dtModuleParamsHelp"].Rows.Clear();

            if (drs == null) return;


            //module info
            DataRow dr = dsInfo.Tables["dtModuleInfo"].NewRow();
            dr["Name"] = "Code";
            dr["Value"] = drs["mod_code"];
            dsInfo.Tables["dtModuleInfo"].Rows.Add(dr);

            dr = dsInfo.Tables["dtModuleInfo"].NewRow();
            dr["Name"] = "Description";
            dr["Value"] = drs["mod_description"];
            dsInfo.Tables["dtModuleInfo"].Rows.Add(dr);

            dr = dsInfo.Tables["dtModuleInfo"].NewRow();
            dr["Name"] = "File";
            dr["Value"] = drs["mod_short_filename"];
            dsInfo.Tables["dtModuleInfo"].Rows.Add(dr);

            dr = dsInfo.Tables["dtModuleInfo"].NewRow();
            dr["Name"] = "Location";
            dr["Value"] = drs["mod_filename"];
            dsInfo.Tables["dtModuleInfo"].Rows.Add(dr);

            dr = dsInfo.Tables["dtModuleInfo"].NewRow();
            dr["Name"] = "Version";
            dr["Value"] = drs["mod_version"];
            dsInfo.Tables["dtModuleInfo"].Rows.Add(dr);

            //module params dtModuleParamsHelp
            _fGroups.dgModuleParams.BeginUpdate();
            try
            {
                XMLInfo xi = Dm4Srv.GetModuleHelp(drs["mod_code"].ToString());
                XMLInfo.FillTable(xi, dsInfo.Tables["dtModuleParamsHelp"]);
            }
            finally
            {
                _fGroups.dgModuleParams.EndUpdate();
            }
        }
    }
}
